package co.edu.javeriana.aes.patrones.monopolio.cliente;

import co.edu.javeriana.aes.patrones.monopolio.message.Message;
import co.edu.javeriana.aes.patrones.monopolio.message.MessageEventListener;
import co.edu.javeriana.aes.patrones.monopolio.message.MessageType;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author gersua
 */
public class WaitPlayers extends AbstractPanel implements MessageEventListener {

    private DefaultListModel listModel;

    /** Creates new form WaitPlayers */
    public WaitPlayers(MonopolyClient monopolyClient, ViewHandler viewHandler) {
        super(monopolyClient, viewHandler);
        listModel = new DefaultListModel();
        monopolyClient.addListener(this);
        initComponents();
        playersList.setModel(listModel);
        playersList.setVisible(false);
        btnStartGame.addActionListener(viewHandler);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        playersList = new javax.swing.JList();
        btnStartGame = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(0, 0, 0));
        setPreferredSize(new java.awt.Dimension(1300, 650));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/javeriana/aes/patrones/monopolio/cliente/resources/monopoly-logo.png"))); // NOI18N

        playersList.setFont(new java.awt.Font("Dialog", 1, 24));
        jScrollPane1.setViewportView(playersList);

        btnStartGame.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
        btnStartGame.setText("Comenzar Partida");
        btnStartGame.setName("btnStartGame"); // NOI18N
        btnStartGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartGameActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 24));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Esperando Jugadores...");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1276, Short.MAX_VALUE)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1276, Short.MAX_VALUE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1276, Short.MAX_VALUE)
                    .add(btnStartGame, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1276, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(btnStartGame, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartGameActionPerformed
    }//GEN-LAST:event_btnStartGameActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStartGame;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList playersList;
    // End of variables declaration//GEN-END:variables

    @Override
    public void handleChatMessage(Message message) {
    }

    @Override
    public void handleRooms(Message message) {
        if (message.getCommand().equals(MessageType.R_PLAYER_JOINED)) {
            listModel.addElement(getMonopolyClient().getSession().getNickmame());
            repaint();
            Message requestPlayers =
                    new Message(getMonopolyClient().getSession(),
                    getMonopolyClient().getSession().getNickmame(),
                    MessageType.R_GET_LIST_OF_PLAYERS);
            getMonopolyClient().sendMessage(requestPlayers);
        }
    }

    @Override
    public void handlePlayerRequest(Message message) {
        if (message.getCommand().equals(MessageType.R_LIST_OF_PLAYERS_RESPONSE)) {
            ArrayList<String> playersNames = (ArrayList<String>) message.getData();
            for (String name : playersNames) {
                listModel.addElement(name);
            }
            repaint();
        }
    }
}
